package com.example.order_system.controller;

import com.example.order_system.model.Dish;
import com.example.order_system.util.ResultData;
import com.example.order_system.model.UserInfo;
import com.example.order_system.service.DishService;
import com.example.order_system.service.OrderDetailService;
import com.example.order_system.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * @description:
 * @author: chengbaiyi
 * @create: 2022-07-22 18:37
 */
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 添加菜品
     * @param name 菜品名称
     * @param price 菜品价格
     * @return ResultData
     */
    @PostMapping("/adddish")
    public ResultData addDish(String name, BigDecimal price) {
        int state = -1;
        String msg = "未知错误";
        int data = -1;
        if (StringUtils.hasLength(name) && StringUtils.hasLength(String.valueOf(price))) {
            data = dishService.addDish(name, price);
            if (data > 0) {
                msg = "添加成功";
                state = 200;
            } else {
                msg = "添加失败";
            }
        } else {
            msg = "参数错误";
        }
        return new ResultData(state, data, msg);
    }

    /**
     * 所有菜品列表
     * @return ResultData
     */
    @GetMapping("/dishlist")
    public ResultData dishList(String isdown) {
        int state = -1;
        List<Dish> data;
        if (StringUtils.hasLength(isdown) && isdown.equals("0")) {
            data = dishService.dishList(0);
        } else {
            data = dishService.dishList(null);
        }
        if (data != null) {
            state = 200;
        }
        return new ResultData(state, data, null);
    }

    /**
     * 删除菜品
     * @param req req
     * @param did 订单id
     * @return ResultData
     */
    @PostMapping("/deldish")
    public ResultData del(HttpServletRequest req, int did) {
        int state = -1;
        int data = -1;
        String msg = "未知错误";
        UserInfo userInfo = SessionUtil.getSessionUserInfo(req);
        if (userInfo != null) {
            if (did > 0) {
                // 删除之前，要先确认订单详情表没有使用此菜品
                // 如果未完成订单中存在该菜品，则删除失败
                int count = orderDetailService.isUsed(did);
                if (count > 0) {
                    // 存在订单，无法删除
                    msg = "删除失败";
                } else {
                    // 可以进行删除
                    data = dishService.del(did);
                    if (data > 0) {
                        state = 200;
                        msg = "删除成功";
                    }
                }
            }
        }
        return new ResultData(state, data, msg);
    }

    /**
     * 下架菜品
     * @param did 菜品id
     * @return ResultData
     */
    @PostMapping("/downdish")
    public ResultData down(Integer did, Integer isdown) {
        int state = -1;
        int data = -1;
        String msg = "未知错误";
        if (did > 0) {
            data = dishService.down(did, isdown);
            if (data > 0) {
                state = 200;
                msg = "操作成功";
            } else {
                msg = "操作失败";
            }
        } else {
            msg = "参数错误";
        }
        return new ResultData(state, data, msg);
    }
}
